<template>
  <div class="app-container">
    <div class="main">
      <div class="left">
        <div class="card">
          <form autocomplete="on" action="#" onsubmit="return false">
            <div class="logo">
              <img src="@/assets/EprJpg/logo.png" alt="" />
            </div>

            <div class="login">
              <div class="user">
                <h5 class="f16">账号</h5>
                <div class="input">
                  <img class="img" src="@/assets/EprJpg/icon/账号登录.svg" alt="" />
                  <input @input="userChange" autocomplete="on" autofocus type="text" placeholder="请输入账号" name="loginName" />
                </div>
              </div>
              <div class="user">
                <h5 class="f16">密码</h5>
                <div class="input">
                  <img class="img" src="@/assets/EprJpg/icon/key.svg" alt="" />
                  <input @input="passwordChange" autocomplete="on" :type="showPassword" placeholder="请输入密码" name="password" />
                  <img @click="eyeClick(eyeClose)" v-if="!eyeClose" class="img2" src="@/assets/EprJpg/icon/eye.svg" alt="" />
                  <img @click="eyeClick(eyeClose)" v-else class="img2" src="@/assets/EprJpg/icon/eye-close.svg" alt="" />
                </div>
              </div>
            </div>
            <footer class="btn">
              <!-- <button type="submit">登录</button> -->
              <input @click="debouncedLaunchForm1" type="submit" value="登录" />
            </footer>
          </form>
        </div>
      </div>
      <div class="right">
        <img src="@/assets/EprJpg/login.jpg" alt="" />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'Login'
}
</script>

<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { useUserInfoStore } from '@/stores/userInfo'
import type { FormInstance } from 'element-plus'
import { nextTick, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import type { LoginPort } from '@/api/login'
import { ElMessage } from 'element-plus'
import { setToken } from '@/utils/token-utils'
import { debounce } from 'lodash'
import JMessage from '@/utils/JMessage'

const user = ref('')
const password = ref('')
const showPassword = ref('password')
const eyeClose = ref(true)
const eyeClick = (eye: boolean) => {
  eye = !eye
  eyeClose.value = eye
  if (eyeClose.value) showPassword.value = 'password'
  else showPassword.value = 'text'
}
const userChange = (e: any) => {
  user.value = e.target.value
}
const passwordChange = (e: any) => {
  password.value = e.target.value
}
const userInfoStore = useUserInfoStore()
const route = useRoute()
const router = useRouter()
const loginForm = ref<LoginPort>({
  loginName: '',
  password: ''
})

const loading = ref(false)
const passwordType = ref('password')
const redirect = ref('')
const passwordRef = ref<HTMLInputElement>()
const formRef = ref<FormInstance>()

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 3) {
    callback(new Error('用户名长度不能小于3位'))
  } else {
    callback()
  }
}

const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 3) {
    callback(new Error('密码长度不能小于3位'))
  } else {
    callback()
  }
}

const loginRules = {
  username: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

watch(
  route,
  () => {
    redirect.value = route.query && (route.query.redirect as string)
  },
  { immediate: true }
)

/* 
点击登陆的回调
*/
const handleLogin = async () => {
  if (loginForm.value.loginName.length === 0 || loginForm.value.password.length === 0) {
    JMessage.error('账号或密码不可为空')
    return
  }
  const result = await userInfoStore.login(loginForm.value)
  if (result === 'ok') {
    JMessage.success('登录成功')
    router.push('/home')
  }
}

const submit = async () => {
  if (user.value === '' || password.value === '') {
    JMessage.warning('账号或密码不可为空')
  } else {
    const result = await userInfoStore.login({
      loginName: user.value,
      password: password.value
    })
    console.log(result)
    if (result === 'ok') {
      JMessage.success('登录成功')
      router.push('/home')
    }
  }
}

const debouncedLaunchForm1 = debounce((formValue: any) => {
  submit()
}, 500) // 2000ms 防抖延迟时间
</script>

<style lang="scss"></style>

<style lang="scss" scoped>
.main {
  height: 100%;
  overflow: hidden;
  background-image: repeating-linear-gradient(#4e4e4e, #181818);
  display: flex;
  justify-content: space-between;

  .left {
    z-index: 1001;
    position: relative;

    .card {
      background-color: white;

      form {
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-evenly;
      }

      .logo {
        display: flex;
        justify-content: center;

        img {
          width: 50%;
        }
      }

      .login {
        width: 80%;
        color: #999999;

        .user {
          margin-bottom: 5%;

          .input {
            margin-top: 20px;
            position: relative;
            display: flex;
            align-items: center;

            input {
              width: 100%;
              height: 55px;
              border: none;
              border-radius: 27px;
              padding-left: 70px;
              font-size: 20px;
              background-color: #f6f6f6;
              outline: solid white 2px;
              transition: outline 0.5s ease-in-out;
              // text-transform: uppercase;
            }

            input::placeholder {
              color: rgba(201, 201, 201, 0.5);
            }

            input:focus-visible {
              box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.11764705882352941);
              outline: solid rgba(248, 144, 39, 1) 2px;
              // border: 2px solid rgba(248, 144, 39, 1);
              background-color: white;
            }

            input::-ms-clear,
            ::-ms-reveal {
              display: none;
            }

            .img {
              width: 30px;
              height: 80%;
              text-align: center;
              display: block;
              position: absolute;
              left: 25px;
            }

            .img2 {
              width: 30px;
              height: 80%;
              text-align: center;
              display: block;
              position: absolute;
              right: 25px;
            }

            .img2:hover {
              cursor: pointer;
            }
          }
        }

        .save {
          display: flex;
          justify-content: space-between;

          .item {
            display: flex;
            align-items: center;
          }

          .item:hover {
            cursor: pointer;
          }

          .img {
            width: 30px;
            height: 30px;
            margin-right: 10px;
          }
        }
      }

      .btn {
        width: 80%;

        input {
          width: 100%;
          height: 55px;
          background-color: #f89027;
          border: 1px solid #f89027;
          color: white;
          border-radius: 27px;
          font-size: 20px;
          box-shadow: 0px 3px 10px rgba(252, 127, 0, 0.4549019607843137);
        }

        input:hover {
          cursor: pointer;
        }
      }
    }
  }

  .right {
    width: 60vw;
    height: 100%;
    position: relative;
    overflow: hidden;

    img {
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      transition: transform 1s ease-out;
    }

    img:hover {
      transform: scale(1.5);
      // animation-duration: 4s;
      // animation-name: scaleDraw;
    }
  }
}

@media screen and (max-width: 750px) {
  .card {
    width: 100vw;
    height: 100vh;
  }
}

@media screen and (min-width: 750px) {
  .card {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(15%, -50%);
    // margin-left: 18%;
    width: 40vw;
    height: 80vh;
    border-radius: 10px;
  }
}

@keyframes scaleDraw {
  /*定义关键帧、scaleDrew是需要绑定到选择器的关键帧名称*/
  0% {
    transform: scale(1);
  }

  // 25% {
  //   transform: scale(1.1);
  //   /*放大1.1倍*/
  // }

  // 50% {
  //   transform: scale(1);
  // }

  100% {
    transform: scale(1.2);
  }

  0% {
    transform: scale(1);
  }
}
</style>
